package se.miun.dt156g.antonsskafferibestallning;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SendOrderActivity extends Activity implements OnClickListener {

	private static final String POST_COMMENT_URL = "http://pellos.no-ip.org/android/adddinnerorder.php";

	private ArrayList<String> dessertNamesArrayList;
	private HashMap<String, Integer> noOfDessertsHashMap;
	private ArrayList<String> dinnerNamesArrayList;
	private HashMap<String, Integer> noOfDinnersHashMap;
	private ArrayList<String> apetizersNamesArrayList;
	private HashMap<String, Integer> noOfApetizersHashMap;
	private ArrayList<String> drinkNamesArrayList;
	private HashMap<String, Integer> noOfDrinksHashMap;
	private int tableNo;

	TextView tableOverview;
	TextView drinksOverview;
	TextView apetizersOverview;
	TextView mainCourseOverview;
	TextView dessertsOverview;

	JSONParser jsonParser = new JSONParser();
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_MESSAGE = "message";

	TextView orderSent;
	Button sendButton;

	private ProgressDialog pDialog;

	private String drinks = "";
	private String apetizers = "";
	private String mainCourses = "";
	private String desserts = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send_order);

		Bundle extras = getIntent().getExtras();

		drinkNamesArrayList = extras.getStringArrayList("drinksArrayList");
		noOfDrinksHashMap = (HashMap<String, Integer>) extras
				.get("drinksHashMap");
		apetizersNamesArrayList = extras
				.getStringArrayList("apetizersArrayList");
		noOfApetizersHashMap = (HashMap<String, Integer>) extras
				.get("apetizersHashMap");
		dinnerNamesArrayList = extras.getStringArrayList("dinnersArrayList");
		noOfDinnersHashMap = (HashMap<String, Integer>) extras
				.get("dinnersHashMap");
		dessertNamesArrayList = extras.getStringArrayList("dessertsArrayList");
		noOfDessertsHashMap = (HashMap<String, Integer>) extras
				.get("dessertsHashMap");
		tableNo = extras.getInt("tableno");

		TextView tableOverview = (TextView) findViewById(R.id.tableOverview);
		TextView drinksOverview = (TextView) findViewById(R.id.drinksOverview);
		TextView apetizersOverview = (TextView) findViewById(R.id.apetizersOverview);
		TextView mainCourseOverview = (TextView) findViewById(R.id.mainCoursesOverview);
		TextView dessertsOverview = (TextView) findViewById(R.id.dessertsOverview);

		sendButton = (Button) findViewById(R.id.sendOrderButton);
		sendButton.setOnClickListener(this);

		tableOverview.setText("Bord: " + Integer.toString(tableNo) + "\n");

		for (int i = 0; i < drinkNamesArrayList.size(); i++) {
			if (noOfDrinksHashMap.get(drinkNamesArrayList.get(i)) > 0) {
				drinks += drinkNamesArrayList.get(i)
						+ " "
						+ Integer.toString(noOfDrinksHashMap
								.get(drinkNamesArrayList.get(i))) + "\n";
			}
		}
		drinksOverview.setText(drinks);

		for (int i = 0; i < apetizersNamesArrayList.size(); i++) {
			if (noOfApetizersHashMap.get(apetizersNamesArrayList.get(i)) > 0) {
				apetizers += apetizersNamesArrayList.get(i)
						+ " "
						+ Integer.toString(noOfApetizersHashMap
								.get(apetizersNamesArrayList.get(i))) + "\n";
			}
		}
		apetizersOverview.setText(apetizers);

		for (int i = 0; i < dinnerNamesArrayList.size(); i++) {
			if (noOfDinnersHashMap.get(dinnerNamesArrayList.get(i)) > 0) {
			mainCourses += dinnerNamesArrayList.get(i)
					+ " "
					+ Integer.toString(noOfDinnersHashMap
							.get(dinnerNamesArrayList.get(i))) + "\n";
			}
		}
		mainCourseOverview.setText(mainCourses);

		for (int i = 0; i < dessertNamesArrayList.size(); i++) {
			if (noOfDessertsHashMap.get(dessertNamesArrayList.get(i)) > 0){
			desserts += dessertNamesArrayList.get(i)
					+ " "
					+ Integer.toString(noOfDessertsHashMap
							.get(dessertNamesArrayList.get(i))) + "\n";
			}
		}
		dessertsOverview.setText(desserts);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.send_order, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.sendOrderButton:
			new SendDinnerOrder().execute();
			break;

		case R.id.returnHomeButton:
			Intent startMainActivityIntent = new Intent(this,
					MainActivity.class);
			startActivity(startMainActivityIntent);
			finish();
			break;
		}

	}

	public class SendDinnerOrder extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(SendOrderActivity.this);
			pDialog.setMessage("Skickar order...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		protected String doInBackground(String... params) {

			// Check for success tag
			int success;

			try {
				// Building Parameters
				List<NameValuePair> parameters = new ArrayList<NameValuePair>();
				parameters.add(new BasicNameValuePair("table_no", Integer
						.toString(tableNo)));

				for (int i = 0; i < drinkNamesArrayList.size(); i++) {
					parameters.add(new BasicNameValuePair("beverage_name[]",
							drinkNamesArrayList.get(i)));
					parameters.add(new BasicNameValuePair("beverage_number[]",
							Integer.toString(noOfDrinksHashMap
									.get(drinkNamesArrayList.get(i)))));
				}

				for (int i = 0; i < apetizersNamesArrayList.size(); i++) {
					parameters.add(new BasicNameValuePair("dinner_name[]",
							apetizersNamesArrayList.get(i)));
					parameters.add(new BasicNameValuePair("dinner_number[]",
							Integer.toString(noOfApetizersHashMap
									.get(apetizersNamesArrayList.get(i)))));
				}

				for (int i = 0; i < dinnerNamesArrayList.size(); i++) {
					parameters.add(new BasicNameValuePair("dinner_name[]",
							dinnerNamesArrayList.get(i)));
					parameters.add(new BasicNameValuePair("dinner_number[]",
							Integer.toString(noOfDinnersHashMap
									.get(dinnerNamesArrayList.get(i)))));
				}

				for (int i = 0; i < dessertNamesArrayList.size(); i++) {
					parameters.add(new BasicNameValuePair("dinner_name[]",
							dessertNamesArrayList.get(i)));
					parameters.add(new BasicNameValuePair("dinner_number[]",
							Integer.toString(noOfDessertsHashMap
									.get(dessertNamesArrayList.get(i)))));
				}

				Log.d("request!", "starting");

				// Posting user data to script
				JSONObject json = jsonParser.makeHttpRequest(POST_COMMENT_URL,
						"POST", parameters);

				// full json response
				Log.d("Post Comment attempt", json.toString());

				// json success element
				success = json.getInt(TAG_SUCCESS);
				if (success == 1) {
					Log.d("Comment Added!", json.toString());
					return json.getString(TAG_MESSAGE);
				} else {
					Log.d("Comment Failure!", json.getString(TAG_MESSAGE));
					return json.getString(TAG_MESSAGE);

				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

		protected void onPostExecute(String file_url) {
			pDialog.dismiss();
			sendButton.setVisibility(View.GONE);

			if (file_url != null) {
				Toast.makeText(SendOrderActivity.this, file_url,
						Toast.LENGTH_LONG).show();
			}
		}
	}
}